package com.newsappserver.newsservice.provider.online.controller;

import com.newsappserver.newsservice.provider.online.data.dto.response.FreeApiNewsResponse;
import com.newsappserver.newsservice.provider.online.data.dto.response.FreeApiResponse;
import com.newsappserver.newsservice.provider.online.service.FreeApiNewsService;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.experimental.FieldDefaults;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequiredArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true)
@RequestMapping("/api/freenews")
public class FreeApiNewsController {
    FreeApiNewsService freeApiNewsService;

    // httpie 测试: http :8083/api/freenews/top, type 是必须的
    @GetMapping("/{type}")
    public FreeApiResponse<FreeApiNewsResponse> getNewsWithType(
            @PathVariable("type") String type
    ) {
        return freeApiNewsService.getNewsByType(type);
    }

    @GetMapping("/fetch")
    public String fetchAndSaveNews() {
        freeApiNewsService.fetchAndSaveNews();
        return "News fetched and saved successfully";
    }
}
